package serial;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import camera.CompareImage;

public class ScreenObject {
	private int threshold;
	private int x1 = Integer.MAX_VALUE;
	private int y1 = Integer.MAX_VALUE;
	private int x2;
	private int y2;

	public ScreenObject() {
		
	}

	public ScreenObject(int x1, int y1, int x2, int y2, int threshold) {
		super();
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.threshold = threshold;
	}

	public void addPosition(int x, int y) {
		if (x < x1) {
			x1 = x;
		}
		if (y < y1) {
			y1 = y;
		}
		if (x > x2) {
			x2 = x;
		}
		if (y > y2) {
			y2 = y;
		}
		threshold++;
	}

	public int getThreshold() {
		return threshold;
	}

	public void drawRect(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.BLUE);
		g.drawRect(x1, y1, x2 - x1, y2 - y1);
		g.setColor(c);
	}

	public boolean isLedOn(BufferedImage current) {
		return CompareImage.changedLED(current, x1, y1, x2, y2, 130);
	}

	public boolean isMotorCOn(BufferedImage old, BufferedImage current) {
		double d = (double) threshold * 0.7;
		return CompareImage.changed(old, current, x1, y1, x2, y2, (int) d);
	}

	public boolean isMotorPOn(BufferedImage old, BufferedImage current) {
		double d = (double) threshold * 0.9;
		return CompareImage.changed(old, current, x1, y1, x2, y2, (int) d);
	}
}
